<?php

namespace App\Admin\Forms\Userinfo;

use Encore\Admin\Widgets\Form;
use Encore\Cropper\Crop;
use Illuminate\Http\Request;
use App\Admin\Models\SequenceModel;
use App\Admin\Models\UserModel;
use App\Admin\Models\EmployeeModel;
use App\Admin\Models\PositionModel;
use Encore\Admin\Facades\Admin;

class Basic extends Form
{
    /**
     * The form title.
     *
     * @var string
     */
    public $title = '基本信息';

    /**
     * Handle the form request.
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request)
    {
        $data = \Request::input();
        $id   = !empty($data['employee_id']) ? (int)$data['employee_id'] : 0;
        if ($id) {
            $model = EmployeeModel::find($id);
        } else {
            $model = new EmployeeModel();
        }
        unset($data['employee_id']);

        // 图片处理
        if (!empty($data['avatar'])) {
            $cropper = new Crop('avatar');
            $path    = $cropper->prepare($data['avatar']);
            if (false !== stripos($path, 'images')) {
                $data['avatar'] = $path;
            }
        }

        if (!empty($data['sequence_pid']) && is_array($data['sequence_pid'])) {
            $data['sequence_pid'] = array_filter($data['sequence_pid']);
        }

        if (!empty($data['sequence_id']) && is_array($data['sequence_id'])) {
            $data['sequence_id'] = array_filter($data['sequence_id']);
        }

        $res = false;
        if ($id) {
            $res = $model->update($data);
        } elseif ($model->save($data)) {
            $res = $model->update($data);
        }

        if ($res) {
            admin_toastr('保存成功');
        } else {
            admin_toastr('保存失败', 'error');
        }

        return back();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $employeeId = !empty($this->data()) ? $this->data()['employee_id'] : 0;

        $this->hidden('employee_id', __('ID'));
//        $this->number('order', __('优先级'))->default(1)->setWidth(4, 2)->setDisplay(false)->rules('required|min:1')->required();
        $this->text('name_zh', __('姓名-中文'))->setWidth(2, 2)->rules('required|min:1|max:10')->maxlength(10)
            ->placeholder('输入姓名-中文，字符长度限制在10字符内')->required();
        $this->text('name_en', __('姓名-英文'))->setWidth(2, 2)->rules('required|min:1|max:30')->maxlength(30)
            ->placeholder('输入姓名-英文，字符长度限制在30字符内')->required();

//        $teachers = UserModel::getTeacherList();
//        !empty($teachers) && $teachers = array_column($teachers, 'name', 'id');
//        $this->select('user_id', __('绑定后台登录账号'))->options($teachers)->setWidth(2, 2)->setDisplay(false)->required();
//
//        $sequences = SequenceModel::getSequenceList(['parent_id' => 0]);
//        !empty($sequences) && $sequences = array_column($sequences, 'name_zh', 'sequence_id');
////        $this->select('sequence_pid', __('岗位类别'))->options($sequences)->load('sequence_id', '/admin/sequence_select')->setWidth(2, 2)->setDisplay(false)->required();
//        $this->multipleSelect('sequence_pid', __('岗位类别'))->options($sequences)->setWidth(4, 2)->setDisplay(false)
//            ->load('sequence_id', '/admin/sequence_select')->placeholder('  请选择岗位类别，可多选')->required();
//
//        $sequencePid   = !empty($this->data()) ? $this->data()['sequence_pid'] : 0;
//        $sequenceLists = $sequencePid ? SequenceModel::getSequenceList(['parent_id' => $sequencePid]) : [];
//        !empty($sequenceLists) && $sequenceLists = array_column($sequenceLists, 'name_zh', 'sequence_id');
////        $this->select('sequence_id', __('职称'))->options($sequenceLists)->setWidth(2, 2)->setDisplay(false)->required();
//        $this->multipleSelect('sequence_id', __('职称'))->options($sequenceLists)->setWidth(4, 2)
//            ->setDisplay(false)->placeholder('  请选择职称，可多选')->required();
//
//        // 职位级别选项
//        $positionOptions = PositionModel::getPositionList();
//        !empty($positionOptions) && $positionOptions = array_column($positionOptions, 'name_zh', 'position_id');
//        $this->select('position_id', __('职位级别'))->options($positionOptions)->setWidth(2, 2)->setDisplay(false);

//        $this->cropper('avatar', __('个人照片'))->cRatio(300, 400)->setWidth(6, 2)->help('照片固定宽高比例 3:4');
        $this->email('email', __('邮箱地址'))->rules('nullable|min:1|max:40')->maxlength(40)
            ->placeholder('输入邮箱地址，字符长度限制在40字符内')->setWidth(4, 2);
        $this->text('phone', __('办公电话'))->icon('fa-phone')->rules('nullable|min:1|max:32')->maxlength(32)
            ->placeholder('输入办公电话，字符长度限制在32字符内')->setWidth(4, 2);
        $this->url('personal_homepage', __('个人主页地址'))->rules('nullable|min:1|max:180')->maxlength(180)
            ->placeholder('输入个人主页地址，字符长度限制在180字符内')->setWidth(4, 2);

        $this->text('office_address_zh', __('办公室地址-中文'))->rules('nullable|min:1|max:60')->maxlength(60)
            ->placeholder('输入办公室地址-中文，字符长度限制在60字符内')->setWidth(4, 2);
        $this->text('office_address_en', __('办公室地址-英文'))->rules('nullable|min:1|max:180')->maxlength(180)
            ->placeholder('输入办公室地址-英文，字符长度限制在180字符内')->setWidth(4, 2);

        $this->text('research_direction_zh', __('研究方向-中文'))->rules('nullable|min:1|max:500')->maxlength(500)
            ->placeholder('输入研究方向-中文，字符长度限制在500字符内');
        $this->text('research_direction_en', __('研究方向-英文'))->rules('nullable|min:1|max:1000')->maxlength(1000)
            ->placeholder('输入研究方向-英文，字符长度限制在1000字符内');

        $this->froala('personal_profile_zh', __('个人简介-中文'))->setContentId($employeeId)->setContentType(1)->setContentType(1);
        $this->froala('personal_profile_en', __('个人简介-英文'))->setContentId($employeeId)->setContentType(1)->setContentType(1);

        // 添加样式
        \Encore\Admin\Admin::style('.fileinput-remove {display:none;} .fa-info-circle {margin-left:10px;}');
    }

    /**
     * The data of the form.
     *
     * @return array $data
     */
    public function data()
    {
        $data = EmployeeModel::where('user_id', '=', Admin::user()->id)->first();
        if (empty($data)) {
            return [];
        }

        $data = $data->toArray();

//        if (is_array(json_decode($data['sequence_pid'], true))) {
//            $data['sequence_pid'] = array_filter(json_decode($data['sequence_pid'], true));
//        }
//
//        if (is_array(json_decode($data['sequence_id'], true))) {
//            $data['sequence_id'] = array_filter(json_decode($data['sequence_id'], true));
//        }

        return $data;
    }
}
